package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.query.BpmnInfoQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpmnInfoServiceImpl implements IBpmnInfoService {
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private RepositoryService repositoryService;

    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQuery qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List<BpmnInfo> bpmnInfos = bpmnInfoMapper.selectForList(qo);
        return new TablePageInfo<BpmnInfo>(bpmnInfos);
    }

    @Override
    public BpmnInfo get(Long id) {
        return bpmnInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public void update(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.updateByPrimaryKey(bpmnInfo);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] Ids = Convert.toLongArray(ids);
        for (Long id : Ids) {
            bpmnInfoMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public List<BpmnInfo> list() {
        return bpmnInfoMapper.selectAll();
    }

    @Override
    @Transactional
    public void deploy(String path, String bpmnType, String info) throws FileNotFoundException {
        File bpmnFile=new File(SystemConfig.getProfile(),path);
        Deployment deploy = repositoryService.createDeployment()
                .addInputStream(path, new FileInputStream(bpmnFile))
                .deploy();
        BpmnInfo bpmnInfo = new BpmnInfo();
        bpmnInfo.setBpmnType(bpmnType);
        bpmnInfo.setInfo(info);
        bpmnInfo.setDeploymentId(deploy.getId());
        bpmnInfo.setDeployTime(new Date());
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        bpmnInfo.setActProcessId(processDefinition.getId());
        bpmnInfo.setActProcessKey(processDefinition.getKey());
        bpmnInfo.setBpmnName(processDefinition.getName());
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public InputStream getResourceAsStream(String deploymentId, String type) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploymentId)
                .singleResult();
        if ("xml".equalsIgnoreCase(type)){
            String xmlResourceName = processDefinition.getResourceName();
            return repositoryService.getResourceAsStream(deploymentId,xmlResourceName);
        }else{
            BpmnModel model =repositoryService.getBpmnModel(processDefinition.getId());
            DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            return generator.generateDiagram(model, Collections.EMPTY_LIST,Collections.EMPTY_LIST,"宋体","宋体","宋体");
        }

    }

    @Override
    @Transactional
    public void delete(Long id) {
        BpmnInfo bpmnInfo = this.get(id);
        bpmnInfoMapper.deleteByPrimaryKey(id);
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(bpmnInfo.getDeploymentId())
                .singleResult();
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId(),true);
        String resourceName = processDefinition.getResourceName();
        File file = new File(SystemConfig.getProfile(),resourceName);
        if (file.exists()){
            file.delete();
        }
    }

    @Override
    public BpmnInfo queryByType(String carPackage) {
        return bpmnInfoMapper.queryByType(carPackage);
    }

}
